﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Newtonsoft.Json;
using OneWorks.Extension;

namespace OneWorks.Web.Common
{
    public class Combo
    {
        [JsonProperty(PropertyName = "value")]
        public object Value { get; set; }

        [JsonProperty(PropertyName = "text")]
        public string Text { get; set; }

        [JsonProperty(PropertyName = "selected")]
        public bool Selected { get; set; }
    }

    public enum ComboField
    {
        Text,
        Value
    }

    public class ComboMap<T>
    {
        private readonly Dictionary<ComboField, string> dictionary = new Dictionary<ComboField, string>();

        public ComboMap<T> Map(Expression<Func<T, object>> expression, ComboField field)
        {
            string fieldName = expression.PropertyNameFor();
            if (!dictionary.ContainsKey(field))
                dictionary.Add(field, fieldName);
            else
                dictionary[field] = fieldName;
            return this;
        }

        public Dictionary<ComboField, string> GetDictionary()
        {
            return dictionary;
        }
    }

    public class ComboAttribute : Attribute
    {
        public ComboField ComboField { get; set; }

        public ComboAttribute(ComboField field)
        {
            ComboField = field;
        }
    }
}
